package com.zrgj.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zrgj.domain.*;
import com.zrgj.manager.mapper.ProductInfoMapper;
import com.zrgj.manager.mapper.ProductMapper;
import com.zrgj.manager.mapper.ProductSalesMapper;
import com.zrgj.manager.service.IProductService;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;


@Service
@Component
public class ProductServiceImpl implements IProductService {
    private ProductMapper productMapper;
    private ProductInfoMapper productInfoMapper;
    private ProductSalesMapper productSalesMapper;

    public ProductServiceImpl(ProductMapper productMapper,
                              ProductInfoMapper productInfoMapper,
                              ProductSalesMapper productSalesMapper) {
        this.productMapper = productMapper;
        this.productInfoMapper = productInfoMapper;
        this.productSalesMapper = productSalesMapper;
    }

    @Override
    public void insertPro(Product product, ProductInfo info) {
        productMapper.insertPro(product);

        info.setProductId(product.getProductId());
        productInfoMapper.insertProductInfo(info);

        ProductSales productSales = new ProductSales();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(product.getProductShelvesTime());
        productSales.setMonth(calendar.get(Calendar.MONTH)+1);
        productSales.setProductId(product.getProductId());

        productSalesMapper.insert(productSales);
    }

    @Override
    public List<Product> selectPro(Product product) {
        List<Product> products = productMapper.selectPro(product);
        return products;
    }

    @Override
    public List<Product> selectNew() {
        List<Product> selectNew = productMapper.selectNew();
        return selectNew;
    }

    @Override
    public List<Product> selected() {
        List<Product> selected = productMapper.selected();
        return selected;
    }

    @Override
    public List<ProductVo> selectBest() {
        List<ProductVo> products = productMapper.selectBest();
        return products;
    }

    @Override
    public List<Product> selectByPrimary(Integer primaryId) {
        return productMapper.selectByPrimary(primaryId);
    }

    @Override
    public List<Product> selectSell(Integer primary) {
        return productMapper.selectSell(primary);
    }

    @Override
    public List<ProductPo> selectShopPrimary(Integer primaryId) {
        Product product = new Product();
        product.setPrimaryId(primaryId);
        List<ProductPo> productPos = productMapper.selectShopPrimary(product);
        return productPos;
    }

    @Override
    public ProductPo selectVo(Integer productId) {
        return productMapper.selectVo(productId);
    }

    @Override
    public void update(ProductPo productPo) {
        Product product = new Product();
        ProductInfo productInfo = new ProductInfo();

        product.setProductId(productPo.getProductId());
        product.setProductName(productPo.getProductName());
        product.setProductPrice(productPo.getProductPrice());
        product.setPrimaryId(productPo.getPrimaryId());
        product.setProductPicture(productPo.getProductPicture());
        product.setBrandId(productPo.getBrandId());
        product.setInventoryRemain(productPo.getInventoryRemain());

        productInfo.setProductId(productPo.getProductId());
        productInfo.setProductInfoIntroduction(productPo.getProductInfoIntroduction());

        productMapper.update(product);
        productInfoMapper.update(productInfo);
    }

    @Override
    public void delete(Integer productId) {
        productMapper.delete(productId);
    }

    @Override
    public List<ProductPo> selectKey(String keyWord) {
        List<ProductPo> productPos = productMapper.selectKey(keyWord);
        return productPos;
    }
}
